package com.hero.tweaks.ui.fragments;

import net.margaritov.preference.colorpicker.ColorPickerPreference;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.hero.tweaks.R;
import com.hero.tweaks.ui.activities.HideIcons;
import com.hero.tweaks.util.AxisPreferenceFragment;
import com.hero.tweaks.util.Helpers;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;

public class Statusbar extends AxisPreferenceFragment implements
		Preference.OnPreferenceChangeListener, OnPreferenceClickListener {

	private static final String PREF_STATUSBAR_CLOCK = "pref_statusbar_clock";
	private static final String PREF_STATUSBAR_DATE = "pref_statusbar_date";
	//private static final String PREF_STATUSBAR_CLOCK_AMPM = "pref_statusbar_clock_ampm";
	//private static final String PREF_STATUSBAR_CLOCK_COLOR = "pref_statusbar_clock_color";
	private static final String PREF_STATUSBAR_DATE_COLOR = "pref_statusbar_date_color";
	private static final String PREF_STATUSBAR_ICONS = "pref_statusbar_icons";
	private static final String PREF_STATUSBAR_ICON_COLOR = "pref_statusbar_icon_color";
	//private static final String PREF_STATUSBAR_TRAFFIC = "pref_statusbar_traffic";
	//private static final String PREF_STATUSBAR_TRAFFIC_COLOR = "pref_statusbar_traffic_color";
	//private static final String PREF_STATUSBAR_CPU_USAGE = "pref_statusbar_cpu";
	//private static final String PREF_STATUSBAR_CPU_USAGE_COLOR = "pref_statusbar_cpu_color";
	private static final String PREF_QS_PULLDOWN = "pref_qs_pulldown";
	private static final String PREF_DOUBLE_TAP_SLEEP = "pref_double_tap_sleep";
	private static final String PREF_CLOCK_ONCLICK = "pref_clock_onclick";
	private static final String PREF_BRIGHTNESS_SLIDER = "pref_brightness_slider";
	
	ListPreference mStatusBarClock;
	ListPreference mStatusBarDate;
	//ListPreference mStatusBarClockAmpm;
	//ColorPickerPreference mStatusBarClockColor;
	ColorPickerPreference mStatusBarDateColor;
	Preference mBattery;
	Preference mIcon;
	ColorPickerPreference mStatusBarIconColor;
	//SwitchPreference mStatusBarTraffic;
	//ColorPickerPreference mStatusBarTrafficColor;
	//SwitchPreference mStatusBarCpuUsage;
	//ColorPickerPreference mStatusBarCpuUsageColor;
	SwitchPreference mQsPulldown;
	SwitchPreference mDoubleTapSleep;
	SwitchPreference mClockOnClick;
	SwitchPreference mBrightnessSlider;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.prefs_statusbar);
			
		mStatusBarClock = (ListPreference) findPreference(PREF_STATUSBAR_CLOCK);
		mStatusBarClock.setOnPreferenceChangeListener(this);
		mStatusBarClock.setValue((Settings.System.getInt(getContentResolver(),
				"tweaks_hide_clock", 0)) + "");
		
		mStatusBarDate = (ListPreference) findPreference(PREF_STATUSBAR_DATE);
		mStatusBarDate.setOnPreferenceChangeListener(this);
		mStatusBarDate.setValue((Settings.System.getInt(getContentResolver(),
				"tweaks_hide_date", 0)) + "");

		/*mStatusBarClockAmpm = (ListPreference) findPreference(PREF_STATUSBAR_CLOCK_AMPM);
		mStatusBarClockAmpm.setOnPreferenceChangeListener(this);
		mStatusBarClockAmpm.setValue((Settings.System.getInt(
				getContentResolver(), "tweaks_clock_ampm", 1)) + "");

		mStatusBarClockColor = (ColorPickerPreference) findPreference(PREF_STATUSBAR_CLOCK_COLOR);
		mStatusBarClockColor.setOnPreferenceChangeListener(this);*/

		mStatusBarDateColor = (ColorPickerPreference) findPreference(PREF_STATUSBAR_DATE_COLOR);
		mStatusBarDateColor.setOnPreferenceChangeListener(this);		

		mIcon = findPreference(PREF_STATUSBAR_ICONS);
		mIcon.setOnPreferenceClickListener(this);

		mStatusBarIconColor = (ColorPickerPreference) findPreference(PREF_STATUSBAR_ICON_COLOR);
		mStatusBarIconColor.setOnPreferenceChangeListener(this);
		
		/*mStatusBarTraffic = (SwitchPreference) findPreference(PREF_STATUSBAR_TRAFFIC);
		mStatusBarTraffic.setOnPreferenceChangeListener(this);
		mStatusBarTraffic.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_traffic_view", 0) == 1));

		mStatusBarTrafficColor = (ColorPickerPreference) findPreference(PREF_STATUSBAR_TRAFFIC_COLOR);
		mStatusBarTrafficColor.setOnPreferenceChangeListener(this);*/

		/*mStatusBarCpuUsage = (SwitchPreference) findPreference(PREF_STATUSBAR_CPU_USAGE);
		mStatusBarCpuUsage.setOnPreferenceChangeListener(this);
		mStatusBarCpuUsage.setChecked((Settings.System.getInt(getContentResolver(),
				"tweaks_cpu_usage", 0) == 1));

		mStatusBarCpuUsageColor = (ColorPickerPreference) findPreference(PREF_STATUSBAR_CPU_USAGE_COLOR);
		mStatusBarCpuUsageColor.setOnPreferenceChangeListener(this);*/
		
		mQsPulldown = (SwitchPreference) findPreference(PREF_QS_PULLDOWN);
		mQsPulldown.setOnPreferenceChangeListener(this);
		mQsPulldown.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_qs_pulldown", 0) == 1));
		
		mDoubleTapSleep = (SwitchPreference) findPreference(PREF_DOUBLE_TAP_SLEEP);
		mDoubleTapSleep.setOnPreferenceChangeListener(this);
		mDoubleTapSleep.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_double_tap_sleep", 0) == 1));
		
		mClockOnClick = (SwitchPreference) findPreference(PREF_CLOCK_ONCLICK);
		mClockOnClick.setOnPreferenceChangeListener(this);
		mClockOnClick.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_htcclock_onclick", 0) == 1));
		
		mClockOnClick = (SwitchPreference) findPreference(PREF_BRIGHTNESS_SLIDER);
		mClockOnClick.setOnPreferenceChangeListener(this);
		mClockOnClick.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_china_brightness_slider", 0) == 1));
		
	}
	
	@Override
	public boolean onPreferenceChange(Preference preference, Object objValue) {
		Cr = getActivity().getContentResolver();
		
		//Clock=EQS

		if (preference == mStatusBarClock) {
			int val = Integer.parseInt((String) objValue);
			Settings.System.putInt(getContentResolver(), "tweaks_hide_clock",
					val);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mStatusBarDate) {
			int val = Integer.parseInt((String) objValue);
			Settings.System.putInt(getContentResolver(), "tweaks_hide_date",
					val);
			setHasOptionsMenu(true);
			return true;
			
			/*} else  if (preference == mStatusBarClockAmpm) {
			int val = Integer.parseInt((String) objValue);
			Settings.System.putInt(getContentResolver(), "tweaks_clock_ampm",
					val);
			return true;
			
		} else if (preference == mStatusBarClockColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(), "tweaks_clock_color",
					intHex);
			return true;*/
			
		} else if (preference == mStatusBarDateColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(), "tweaks_date_color",
					intHex);
			return true;
		} else if (preference == mStatusBarIconColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(), "tweaks_notification_icon_color",
					intHex);
			setHasOptionsMenu(true);
			return true;
		/*} else if (preference == mStatusBarTraffic) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_traffic_view",
					value);
			return true;
		} else if (preference == mStatusBarTrafficColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(), "tweaks_traffic_color",
					intHex);
			return true;*/
			
		/*} else if (preference == mStatusBarCpuUsage) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_cpu_usage",
					value);
			return true;
		} else if (preference == mStatusBarCpuUsageColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_cpu_usage_color", intHex);
			return true;*/
		
		} else if (preference == mQsPulldown) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_qs_pulldown",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mDoubleTapSleep) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_double_tap_sleep",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mClockOnClick) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_htcclock_onclick",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mBrightnessSlider) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_china_brightness_slider",
					value);
			setHasOptionsMenu(true);
			return true;
			
		}
			
		return false;
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.layout.actionbar_actions, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case (R.id.restart_sysui):
			Helpers.restartHotreboot();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	
	@Override
	public boolean onPreferenceClick(Preference preference) {
		String key = preference.getKey();
		 if (key.equals(PREF_STATUSBAR_ICONS)) {
			Intent myIntent = new Intent(getActivity(), HideIcons.class);
			getActivity().startActivity(myIntent);
			return true;
		} 
		return false;
	}

}